package com.app.scale.view;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.app.scale.R;
import com.app.scale.basis.BasisActivity;
import com.app.scale.basis.Constants;
import com.app.scale.bean.BaseInfoUpdateCache;
import com.app.scale.bean.BasisBean;
import com.app.scale.bean.GoodsListBean;
import com.app.scale.bean.LoginBean;
import com.app.scale.bean.SupplierListData;
import com.app.scale.bean.UpdateBean;
import com.app.scale.dialog.UpdateBaseInfoDialog;
import com.app.scale.greendao.GreenDaoUtils;
import com.flyco.dialog.widget.NormalDialog;
import com.google.gson.Gson;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.DialogUtils;
import my.MySharedPreferences;
import my.SystemParamsUtils;
import my.TimeUtils;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;


/**
 * @备注 人员状态编辑对话框
 */
public class AddPeopleDialog extends NormalDialog {


	BasisActivity mContext;
	String cno;

	@BindView(R.id.edit_name)
	EditText edit_name;
	@BindView(R.id.edit_cdno)
	EditText edit_cdno;
	@BindView(R.id.edit_ctel)
	EditText edit_ctel;

	onPeopleStatusEditListener mListener;

	public AddPeopleDialog(BasisActivity context, onPeopleStatusEditListener listener) {
		super(context);
		mContext = context;
		mListener = listener;
		setCancelable(true);
		setCanceledOnTouchOutside(false);
	}

	@Override
	public NormalDialog style(int style) {
		return super.style(R.style.dialog_soft_input);
	}

	private void initData() {
		getCno();
	}
	private void getCno(){
		mContext.showLoading();
		MyParams params = new MyParams();
		params.put("aid", LoginBean.getInstance().getAcode());
		params.put("cfsign", "2");
		MyHttpUtils.get(Constants.URL_COMPAY_RETURN_NUM_BY_CNO, params, new MyHttpListener() {
			@Override
			public void onSuccess(int httpWhat, Object result) {
				if(mContext.isFinishing())return;
				BasisBean basisBean = (BasisBean) result;
				cno = basisBean.getResultData();
			}

			@Override
			public void onFailure(int httpWhat, Object result) {
				if(mContext.isFinishing())return;
				mContext.dismissProgress();
				dismiss();
				mContext.showToast("异常。请重新点击！");
			}

			@Override
			public void onFinish(int httpWhat) {
				if(mContext.isFinishing())return;
				mContext.dismissProgress();
			}
			}, 0, BasisBean.class);

	}



	@OnClick({R.id.img_close,R.id.btn_cancel})
	void onCloseClick(){
		if(mListener != null){
			mListener.onCancel();
		}
		dismiss();
	}


	@OnClick(R.id.btn_ok)
	void post() {

		String cna = edit_name.getText().toString().trim();
		if(TextUtils.isEmpty(cna)){
			mListener.onToast("名称不可为空");
			return;
		}

		String cdno = edit_cdno.getText().toString().trim();
		if(TextUtils.isEmpty(cdno)){
			mListener.onToast("卡号不可为空");
			return;
		}
		if(cdno.length()!=10){
			mListener.onToast("卡号位数必须是10位");
			return;
		}

		String ctel = edit_ctel.getText().toString().trim();
		if(TextUtils.isEmpty(cdno)){
			mListener.onToast("电话不可为空");
			return;
		}

		SupplierListData data = new SupplierListData();
		data.setAid(LoginBean.getInstance().getAid());
		data.setPrp("生态茶");
		data.setCfs("代理商");
		data.setAcr("0");
		data.setCfsign("2");
		data.setCno(cno);
		data.setCna(cna);
		data.setCdno(cdno);
		data.setCtel(ctel);


		MyParams params = new MyParams();
		mContext.showProgress();
		Gson gson = new Gson();
		String json = gson.toJson(data);
		params.putJson(json);

		MyHttpUtils.postJson(Constants.URL_COMPAY_INSERT, params, new MyHttpListener() {
			@Override
			public void onSuccess(int httpWhat, Object result) {
				if(mContext.isFinishing())return;
				BasisBean basisBean = (BasisBean) result;
				GreenDaoUtils.supplierBeanReplace(data, new GreenDaoUtils.GreenDaoInserCallBack() {
					@Override
					public void onDataInsert(int count) {
						if(mListener != null){
							mListener.onToast("添加成功！");
						}
					}
				});

			}

			@Override
			public void onFailure(int httpWhat, Object result) {
				if(mContext.isFinishing())return;
				if(result != null &&   !TextUtils.isEmpty(((BasisBean) result).getStatusInfo())){
					DialogUtils.showErrorDialog(mContext,((BasisBean) result).getStatusInfo());
				}else {
					DialogUtils.showErrorDialog(mContext,"异常！");
				}
			}

			@Override
			public void onFinish(int httpWhat) {
				if(mContext.isFinishing())return;
				mContext.dismissProgress();
				dismiss();
			}
		}, 0, BasisBean.class);


	}

	@Override
	public View onCreateView() {
		View inflate = LayoutInflater.from(mContext).inflate(R.layout.add_people_dialog, null);
		ButterKnife.bind(this, inflate);
		widthScale(0.6f);
		heightScale(1.0f);
		return inflate;
	}
	@Override
	public void setUiBeforShow() {
		initData();
	}

	public interface onPeopleStatusEditListener {
		abstract void onCancel();
		abstract void onToast(String mes);
	}
}
